cmake_minimum_required(VERSION 3.15)
project(video_tracker)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

option(USE_HW_CODEC "use hardware accelerator" ON)
option(USE_TOPS_FFMPEG "use Tops ffmpeg" ON)


#set(CMAKE_CXX_FLAGS -fsanitize=address ${CMAKE_CXX_FLAGS})
#add_subdirectory(../otl otl.dir)

# Find required packages
set(CMAKE_PREFIX_PATH $ENV{HOME}/buildroot ${CMAKE_PREFIX_PATH})
find_package(OpenCV REQUIRED)

if (NOT USE_DUMMY_DETECTOR)
    # Find TopsInference
    find_path(TOPSINFERENCE_INCLUDE_DIR TopsInference/TopsInferRuntime.h
            PATHS /opt/tops/include $ENV{HOME}/enrigin_sdk/usr/include
            DOC "TopsInference include directory")

    find_path(TOPS_EXT_INCLUDE_DIR tops/tops_ext.h
            PATHS /opt/tops/include $ENV{HOME}/enrigin_sdk/opt/tops/include
            DOC "TopsInference include directory")

    find_library(TOPSINFERENCE_LIBRARY TopsInference
            PATHS /lib /opt/tops/lib $ENV{HOME}/enrigin_sdk/usr/lib
            DOC "TopsInference library")

    find_library(TOPSRT_LIBRARY topsrt
            PATHS /opt/tops/lib $ENV{HOME}/enrigin_sdk/usr/lib
            DOC "TopsRT library")
endif()

if(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
    message(STATUS "detected macOS system")
    # macOS 特定配置
    #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
    # 示例：链接 macOS 框架
    # find_library(CORE_FOUNDATION CoreFoundation)
    # target_link_libraries(your_target PRIVATE ${CORE_FOUNDATION)
    include_directories(/opt/homebrew/include)
elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
    message(STATUS "detected Linux system")
elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
    message(STATUS "detected  Windows system")
else()
	message(STATUS "Unknown：${CMAKE_SYSTEM_NAME}")
endif()

if (NOT USE_TOPS_FFMPEG)
    # Find FFmpeg using pkg-config
    find_package(PkgConfig REQUIRED)
    pkg_check_modules(FFMPEG REQUIRED IMPORTED_TARGET
            libavcodec
            libavformat
            libavfilter
            libavutil
            libswscale
            libavdevice
    )
   
    set(FFMPEG_LINK_LIBS PkgConfig::FFMPEG)

else()
    set(FFMPEG_INCLUDE_DIRS $ENV{HOME}/buildroot/include)
    set(FFMPEG_LINK_DIRS /opt/tops/lib $ENV{HOME}/enrigin_sdk/opt/tops/lib)
    set(FFMPEG_LINK_LIBS avfilter avdevice avformat avcodec swresample swscale avutil)
endif()

set(TRACKER_INC_DIRS
        /usr/include/eigen3
        ${CMAKE_CURRENT_SOURCE_DIR}/../otl
        ${CMAKE_CURRENT_SOURCE_DIR}/include
        ${TOPSINFERENCE_INCLUDE_DIR}/include
        ${TOPS_EXT_INCLUDE_DIR}
        ${FFMPEG_INCLUDE_DIRS}
)
message(STATUS "TRACKER_INC_DIRS=${TRACKER_INC_DIRS}")
include_directories(
	${TRACKER_INC_DIRS}
)

set(LINK_DIRS ${FFMPEG_LINK_DIRS} ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
message(STATUS "tracker:${LINK_DIRS}")
link_directories(${FFMPEG_LINK_DIRS} ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})

set(TRACKER_SRCS
        BYTETracker.cpp
        kalmanFilter.cpp
        lapjv.cpp
        STrack.cpp
        utils.cpp
)

message(STATUS "tracker_SRC=${TRACKER_SRCS}")
# Add source files
add_library(video_tracker SHARED
        ${TRACKER_SRCS}
)

# Include directories
target_include_directories(video_tracker
        PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}/include
        ${OpenCV_INCLUDE_DIRS}
        ${TOPSINFERENCE_INCLUDE_DIR}
)

# Link libraries
target_link_libraries(video_tracker
        PRIVATE
        otl
        ${TOPSINFERENCE_LIBRARY}
        ${TOPSRT_LIBRARY}
        ${OpenCV_LIBS}
        ${FFMPEG_LINK_LIBS}
        pthread
)
